<?php
//客户管理类
class ClientAction extends CommonAction {
	public function index() {
		parent::userauth2(89);
		$this->getsearchparam();
		$data = $this->zuhewhere();

		//重组$data
		$data['approveStatus'] = isset($data['approveStatus'])?$data['approveStatus']:-1;
		$data['applyDatetime'] = isset($data['applyDatetime'])?$data['applyDatetime']:-1;
		$data['sort'] = isset($data['sort'])?$data['sort']:1;
		$data['headFlag'] = isset($data['headFlag'])?$data['headFlag']:'';
		$data['keyWord'] = $data['keyWord']?$data['keyWord']:'';
		$keyWord = $data['keyWord'];
		$data['start'] = $data['start']?$data['start']:0;
		$data['pageSize'] = 15;
		//dump($data);
		//请求列表
		$html = send_post(C('URL').'/manager/user/getClientInfo',$data);
		$count = $html['data']['count'];
		$user = $html['data']['userList'];
		//dump($html);
		import('ORG.Util.Page');				// 导入分页类
		$pagenum = isset ( $_REQUEST ['page'] ) ? $_REQUEST ['page'] : 1;
		$page_row = 15;//每页显示条数
		//dump($count);
		$p = new Page ($count,$page_row,8);

		$p->isJumpPage = true;
		$p->setConfig('theme','%totalRow% %header% %nowPage%/%totalPage% 页%first% %upPage% %linkPage% %downPage% %end% %jump%');
		$show = $p->show();
		//dump($show);
		$this->assign('user',$user);
		// $this->assign('maxpage',$maxpage);
		$this->assign('key',$keyWord);
		$this->assign('page',$show);
		$this->assign('newsurl',C('NEWSURL'));
		$this->assign('url',C('URL'));
		$this->assign('admin',$_SESSION['ThinkUser']);
		$this->display();
	}

	//查询商户及代理商
	public function agent() {

		//清空区域
		if($_GET['clear'] == 1){
			cookie('state','');
			cookie('city','');
			cookie('area','');
		}
		$this->getsearchparam();
		$data = $this->zuhewhere2();

		// if($data['position_id'] && !$data['position_id'] == '-1'){
		// 	$where['position_id'] = $data['position_id'];
		// }
		// if($data['time'] == 1){
		// 	$order['create_time'] = 'DESC';
		// }else{
		// 	$order['create_time'] = 'ASC';
		// }

		// $address = $_COOKIE['state'].$_COOKIE['city'].$_COOKIE['area'];
		// //dump($address);
		// $where['company_address'] = array('like',"%$address%");
		// $where['_string'] = "(company_name like '%$data[keyWord]%')  OR ( name like '%$data[keyWord]%')";
		// $agent = M('agent');
		// import('ORG.Util.Page');		// 导入分页类
		// $pagenum = isset( $_REQUEST ['page'] ) ? $_REQUEST ['page'] : 1;
		// $page_row = 15;//每页显示条数
		// $count = $agent->where($where)->count();
		// $p = new Page ($count,$page_row,8);
		// $p->isJumpPage = true;
		// $p->setConfig('theme','%totalRow% %header% %nowPage%/%totalPage% 页%first% %upPage% %linkPage% %downPage% %end% %jump%');
		// $show = $p->show();
		// $agents = $agent->where($where)->order($order)->limit(($pagenum-1)*$page_row.','.$page_row)->having()->select();
	
		//重组$data
		$keyWord = $data['keyWord'];
		$data['companyName'] = $data['keyWord']?$data['keyWord']:'';
		$data['companyAddress'] = $_COOKIE['state'].$_COOKIE['city'].$_COOKIE['area'];
		$data['userRole'] = isset($data['userRole'])?$data['userRole']:'0';
		$data['isUse'] = isset($data['isUse'])?$data['isUse']:'-1';
		$data['sort'] = isset($data['sort'])?$data['sort']:'0';
		$data['start'] = $data['start']?$data['start']:0;
		$data['pageSize'] = 15;
		//dump($data);
		//请求列表
		$html = send_post(C('URL').'/manager/community/queryCommunityUser',$data);
		//dump($html);

		$count = $html['data']['count'];
		import('ORG.Util.Page');				// 导入分页类
		$pagenum = isset ( $_REQUEST ['page'] ) ? $_REQUEST ['page'] : 1;
		$page_row = 15;//每页显示条数
		//dump($count);
		$p = new Page ($count,$page_row,8);

		$p->isJumpPage = true;
		$p->setConfig('theme','%totalRow% %header% %nowPage%/%totalPage% 页%first% %upPage% %linkPage% %downPage% %end% %jump%');
		$show = $p->show();

		//dump($html['data']);
		$this->assign('agents',$html['data']['communityList']);
		$this->assign('page',$show);
		$this->assign('state',$_COOKIE['state']);
		$this->assign('city',$_COOKIE['city']);
		$this->assign('area',$_COOKIE['area']);
		$this->assign('key',$keyWord);
		$this->display();
	}

	//用户详情信息
	public function user(){
		parent::userauth2(89);
		$data['sid'] =  $_GET['sid'];
		//请求列表
		$html = send_post(C('URL').'/manager/user/userDetail',$data);
		$user = $html['data']['user'];
		$this->assign('user',$user);
		$this->assign('url',C('URL'));
		$this->display();
	}

	public function nopa(){
		$nopa = $_GET['_URL_'];
		$this->assign('url',C('URL'));
		$this->assign('nopa',$nopa);
		$this->display();
	}

	//时间美化函数
	protected function Beautifytime($dtime) {
		$dtime = strtotime($dtime);
		$betime = time()-$dtime;
		$timename='';
		switch($betime) {
			case ($betime < 60):
				$timename = floor($betime).'秒前';
				break;
			case ($betime < 3600 && $betime > 60):
				$timename = floor(($betime/60)).'分钟前';
				break;
			case ($betime < 86400 && $betime > 3600):
				$timename = floor(($betime/60/60)).'小时前';
				break;
			case ($betime < 2592000 && $betime > 86400):
				$timename = floor(($betime/60/60/30)).'天前';
				break;
			case ($betime < 31536000 && $betime > 2592000):
				$timename = floor(($betime/60/60/30/12)).'个月前';
				break;
			case ($betime < 3153600000 && $betime > 31536000):
				$timename = floor(($betime/60/60/30/12/100)).'年前';
				break;
		}
		return $timename;
	}

	public function getsearchparam(){
		//获取会员状态
		$this->assign('status', D('status')->field('id, name, value')->order('id asc')->where('pid = 1')->select());
		//申请时间
		$this->assign('times', D('status')->field('id, name, value')->order('id asc')->where('pid = 2')->select());
		//排序方式
		$this->assign('sort', D('status')->field('id, name, value')->order('id asc')->where('pid = 7')->select());
		//专员分类
		// $this->assign('user', D('status')->field('id, name, value')->order('id asc')->where('pid = 8')->select());
		// dump(D('status')->field('id, name, value')->order('id asc')->where('pid = 8')->select());

		//专员头像
		$this->assign('userimg', D('status')->field('id, name, value')->order('id asc')->where('pid = 9')->select());
		// dump(D('status')->field('id, name, value')->order('id asc')->where('pid = 9')->select());

		//商户分类
		$this->assign('agent',D('status')->field('id,name,value')->order('id asc')->where('pid = 13')->select());

		//启用停用
		$this->assign('enable',D('status')->field('id,name,value')->order('id asc')->where('pid = 14')->select());

		//升序降序
		$this->assign('asc',D('status')->field('id,name,value')->order('id asc')->where('pid = 15')->select());

		//合同状态
		$this->assign('contract',D('status')->field('id,name,value')->order('id asc')->where('pid = 19')->select());

		//日期
		$this->assign('datetime', D('status')->field('id, name, value')->order('id asc')->where('pid = 20')->select());

		//升序降序
		$this->assign('desc',D('status')->field('id,name,value')->order('id asc')->where('pid = 21')->select());
	}

	//组合请求数据逸专员
	public function zuhewhere(){
		$data = array();
		//分页
		if(isset($_GET['page']) && intval($_GET['page']) >= 0){
			$data['start'] =  (intval($_GET['page'])-1)*15;
		}

		//会员状态
		if(isset($_GET['s'])){
			$data['approveStatus'] = intval($_GET['s']);
			if($data['approveStatus'] !== $_SESSION['approveStatus']){
					unset($_REQUEST['page']);
					$data['start'] = 0;	

			}
			$_SESSION['approveStatus'] = intval($_GET['s']);	
		}

		//申请时间
		if(isset($_GET['t'])){
			$data['applyDatetime'] =  intval($_GET['t']);
			if($data['applyDatetime'] !== $_SESSION['applyDatetime']){
					$data['start'] = 0;
					unset($_REQUEST['page']);
			}
			$_SESSION['applyDatetime'] = intval($_GET['t']);
			
		}

		//排序方式
		if(isset($_GET['p'])){
			$data['sort'] =  intval($_GET['p']);
			if($data['sort'] !== $_SESSION['sort']){
					$data['start'] = 0;
					unset($_REQUEST['page']);
			}
			$_SESSION['sort'] = intval($_GET['p']);
			
		}

		//是否有头像
		if(isset($_GET['g'])){
			$data['headFlag'] =  intval($_GET['g']);
			if($data['headFlag'] !== $_SESSION['headFlag']){
					$data['start'] = 0;
					unset($_REQUEST['page']);
			}
			$_SESSION['headFlag'] = intval($_GET['g']);
			
		}

		if(isset($_GET['k'])){
			//过滤KEY
			$key = trim($_GET['k']);
			if (!get_magic_quotes_gpc()){
				$key = addslashes($key);
			}
			$key = str_replace('_', '', $key);
			$key = str_replace('%', '', $key);
			$key = str_replace('-', '', $key);
			$key = str_replace('*', '', $key);
			$encode = mb_detect_encoding($key, array("ASCII","UTF-8","GB2312","GBK","BIG5"));//mb_detect_encoding检测不准,用这句可以准确检测
			if($encode == "UTF-8"){
				$this->assign('searchkey',$key);
			}else{
				$this->assign('searchkey',iconv('gbk','utf-8',$key));
				$key = iconv('gbk','utf-8',$key);
			}
			if($key != '')
				$data['keyWord'] = $key;
				if($data['keyWord'] !== $_SESSION['keyWord']){
					$data['start'] = 0;
					unset($_REQUEST['page']);
				}
				$_SESSION['keyWord'] = $key;
		}
		//dump($_GET['page']);
		return $data;
	}


	//组合条件 商户及代理商
	public function zuhewhere2(){
		cookie('state',$_GET['state']);
		cookie('city',$_GET['city']);
		cookie('area',$_GET['area']);

		$data = array();
		//分页
		if(isset($_GET['page']) && intval($_GET['page']) >= 0){
			$data['start'] =  (intval($_GET['page'])-1)*15;
		}

		//用户类型
		if(isset($_GET['n'])){
			$data['userRole'] = intval($_GET['n']);
		}

		//状态
		if(isset($_GET['e'])){
			$data['isUse'] =  intval($_GET['e']);
		}

		//排序方式
		if(isset($_GET['c'])){
			$data['sort'] =  intval($_GET['c']);	
		}

		if(isset($_GET['k'])){
			//过滤KEY
			$key = trim($_GET['k']);
			if (!get_magic_quotes_gpc()){
				$key = addslashes($key);
			}
			$key = str_replace('_', '', $key);
			$key = str_replace('%', '', $key);
			$key = str_replace('-', '', $key);
			$key = str_replace('*', '', $key);
			$encode = mb_detect_encoding($key, array("ASCII","UTF-8","GB2312","GBK","BIG5"));//mb_detect_encoding检测不准,用这句可以准确检测
			if($encode == "UTF-8"){
				$this->assign('searchkey',$key);
			}else{
				$this->assign('searchkey',iconv('gbk','utf-8',$key));
				$key = iconv('gbk','utf-8',$key);
			}
			if($key != '')
				$data['keyWord'] = $key;
		}
		return $data;
	}

	//登录日志
	public function login(){

		$data['userName'] =  $_GET['userNickName'];
		//请求列表
		$html = send_post(C('URL').'/manager/loginLog/queryList',$data);
		$login = $html['data']['objectList'];
		$count = $html['data']['allCount'];
		//dump($user);
		import('ORG.Util.Page');				// 导入分页类
		$Page = new Page($count,2);				//实例化分页类 传入总记录数和每页显示的记录数
		$Page->setConfig('header','条记录');
		$Page->setConfig('prev','<img src="__IMAGE__/prev.gif" border="0" title="上一页" />');
		$Page->setConfig('next','<img src="__IMAGE__/next.gif" border="0" title="下一页" />');
		$Page->setConfig('first','<img src="__IMAGE__/first.gif" border="0" title="第一页" />');
		$Page->setConfig('last','<img src="__IMAGE__/last.gif" border="0" title="最后一页" />');
		$show = $Page->show();
		$this->assign('page',$show);
		$this->assign('login',$login);
		$this->assign('url',C('URL'));
		$this->display();
	}

	//修改用户信息
	public function edit(){

		$this->assign('url',C('URL'));
		$this->display();

	}


	//修改商户信息
	public function agentedit(){
		$sid = I('get.sid');
		$mysql = "mysql://eachonline_crm:Eachonline_crm@2016@192.168.0.5:33066/eachonline_crm";
		if(I('get.userRole') == 1){
			$sql = "SELECT count(1) FROM crm_user WHERE isagent = 3 AND general_agent = '$sid'";
			$area_agent = M("User","crm_",$mysql)->query($sql);
			$sql2 = "SELECT count(1) FROM crm_user as u, crm_role as r WHERE u.user_id = r.user_id AND r.position_id = 19 AND u.isagent = 0 AND u.general_agent = '$sid'";
			$company = M("User","crm_",$mysql)->query($sql2);
		}else if(I('get.userRole') == 4){
			$area_agent['0']['count(1)'] = 0;
			$sql = "SELECT count(1) FROM crm_user as u,crm_role as r WHERE u.user_id = r.user_id AND r.position_id = 19 AND u.isagent = 0 AND area_agent = '$sid'";
			$company = M("User","crm_",$mysql)->query($sql);
		}else{
			$area_agent['0']['count(1)'] = 0;
			$company['0']['count(1)'] = 0;
		}
		$data['userId'] = $sid;
		$html = send_post(C('URL').'/manager/community/queryCommunityUserDetail',$data);

		$this->assign('type',I('get.type'));
		$this->assign('userid',$sid);
		$this->assign('cc',$html['data']);
		$this->assign('agent',$area_agent['0']['count(1)']);
		$this->assign('company',$company['0']['count(1)']);
		$this->assign('url',C('URL'));
		$this->display();
	}

	//查看用户详情
	public function agentview(){
		$agent = M('agent');
		$user = $agent->where("sid = '%s'",get('sid'))->find();
		$this->assign('user',$user);
		$this->assign('webimg',C('WEBIMG'));
		$this->display();
	}

	//申请成为代理商
	public function apply(){
		$apply = M('apply');
		import('ORG.Util.Page');		// 导入分页类
		$pagenum = isset( $_REQUEST ['page'] ) ? $_REQUEST ['page'] : 1;
		$page_row = 15;//每页显示条数
		$count = $apply->where("name like \"%$_POST[name]%\"")->count();
		$p = new Page ($count,$page_row,8);
		$p->isJumpPage = true;
		$p->setConfig('theme','%totalRow% %header% %nowPage%/%totalPage% 页%first% %upPage% %linkPage% %downPage% %end% %jump%');
		$show = $p->show();
		$user = $apply->where("name like \"%$_POST[name]%\"")->limit(($pagenum-1)*$page_row.','.$page_row)->order('time asc')->select();
		$this->assign('page',$show);
		$this->assign('user',$user);
		$this->display();
	}

	//区域代理合同审核
	public function local(){
		$this->getsearchparam();
		$data['checkStatus'] = isset($_GET['s']) ? $_GET['s'] : '';
		$data['datetime'] = isset($_GET['t']) ? $_GET['t']: '';
		$data['sort'] = isset($_GET['d']) ? $_GET['d']: '0';
		$data['keyWord'] = isset($_GET['k']) ? $_GET['k']:'';
		$data['pageSize'] = 15;
		$data['pageNum'] = isset($_GET['page'])?$_GET['page']:'1';
		$html = send_post(C('URL').'/manager/contract/findAgentContract',$data);
		$count = $html['data']['totalRowNumber'];
		import('ORG.Util.Page');				// 导入分页类
		$pagenum = isset ( $_REQUEST ['page'] ) ? $_REQUEST ['page'] : 1;
		$page_row = 15;//每页显示条数
		//dump($count);
		$p = new Page ($count,$page_row,8);
		$p->isJumpPage = true;
		$p->setConfig('theme','%totalRow% %header% %nowPage%/%totalPage% 页%first% %upPage% %linkPage% %downPage% %end% %jump%');
		$show = $p->show();
		$this->assign('page',$show);
		$this->assign('local',$html['data']['dataList']);
		$this->assign('key',$data['key']);
		$this->assign('url',C('URL'));
		$this->assign('checkName',$_SESSION['ThinkUser']['Username']);
		$this->display();
	}


	//区域代理商管理
	public function useragent(){
		$this->getsearchparam();
		$data['datetime'] = isset($_GET['t']) ? $_GET['t']: '';
		$data['sort'] = isset($_GET['d']) ? $_GET['d']: '0';
		$data['keyWord'] = isset($_GET['k']) ? $_GET['k']:'';
		$data['pageSize'] = 15;
		$data['pageNum'] = isset($_GET['page'])?$_GET['page']:'1';
		$html = send_post(C('URL').'/manager/contract/queryAgentContract',$data);
		$count = $html['data']['totalRowNumber'];
		import('ORG.Util.Page');				// 导入分页类
		$pagenum = isset ( $_REQUEST ['page'] ) ? $_REQUEST ['page'] : 1;
		$page_row = 15;//每页显示条数
		//dump($count);
		$p = new Page ($count,$page_row,8);
		$p->isJumpPage = true;
		$p->setConfig('theme','%totalRow% %header% %nowPage%/%totalPage% 页%first% %upPage% %linkPage% %downPage% %end% %jump%');
		$show = $p->show();
		$this->assign('page',$show);
		$this->assign('local',$html['data']['dataList']);
		$this->assign('key',$data['key']);
		$this->assign('url',C('URL'));
		$this->assign('checkName',$_SESSION['ThinkUser']['Username']);
		$this->display();
	}
      

    //合同管理
    public function contract(){
    	$data['userId'] = $_GET['userId'];
    	$html = send_post(C('URL').'/manager/contract/queryUserAgentContract',$data);
    	$this->assign('contract',$html['data']);
    	$this->assign('url',C('URL'));
		$this->assign('checkName',$_SESSION['ThinkUser']['Username']);
    	$this->display();
    }   

    //添加合同
    public function addcontract(){
    	$this->assign('url',C('URL'));
    	$this->assign('userId',$_GET['sid']);
    	$this->display();
    }

    public function contractOne(){
    	$data['userId'] = $_GET['userId'];
    	$data['contractNo'] = $_GET['contractNo'];
    	$html = send_post(C('URL').'/manager/contract/getAgentContract',$data);
    	$startDate = explode("-",date("Y-m-d",strtotime($html['data']['startDate'])));
    	$endDate = explode("-",date("Y-m-d",strtotime($html['data']['endDate'])));
    	$this->assign('endDate',$endDate);
    	$this->assign('startDate',$startDate);
    	$this->assign('contract',$html['data']);
    	$this->display();
    }

    public function contractTwo(){
    	$data['userId'] = $_GET['userId'];
    	$data['contractNo'] = $_GET['contractNo'];
    	$html = send_post(C('URL').'/manager/contract/getAgentContract',$data);
    	$startDate = explode("-",date("Y-m-d",strtotime($html['data']['startDate'])));
    	$endDate = explode("-",date("Y-m-d",strtotime($html['data']['endDate'])));
    	$this->assign('endDate',$endDate);
    	$this->assign('startDate',$startDate);
    	$this->assign('contract',$html['data']);
    	$this->display();
    }

    public function contractThree(){
    	$data['userId'] = $_GET['userId'];
    	$data['contractNo'] = $_GET['contractNo'];
    	$html = send_post(C('URL').'/manager/contract/getAgentContract',$data);
    	$startDate = explode("-",date("Y-m-d",strtotime($html['data']['startDate'])));
    	$endDate = explode("-",date("Y-m-d",strtotime($html['data']['endDate'])));
    	$this->assign('endDate',$endDate);
    	$this->assign('startDate',$startDate);
    	$this->assign('contract',$html['data']);
    	$this->display();
    }

    public function contractFour(){
    	$data['userId'] = $_GET['userId'];
    	$data['contractNo'] = $_GET['contractNo'];
    	$html = send_post(C('URL').'/manager/contract/getAgentContract',$data);
    	$startDate = explode("-",date("Y-m-d",strtotime($html['data']['startDate'])));
    	$endDate = explode("-",date("Y-m-d",strtotime($html['data']['endDate'])));
    	$this->assign('endDate',$endDate);
    	$this->assign('startDate',$startDate);
    	$this->assign('contract',$html['data']);
    	$this->display();
    }



    /**
	 * 审核入驻资料
	 */
    public function check(){
//		$this->getsearchparam();
//		$status=D('status')->field('id,name,value')->order('id asc')->where('pid = 19')->select();
//		foreach($status as $k=>$v){
//			if($v['value']==3){
//				unset($status[$k]);
//			}
//		}
//		$this->assign('status',$status);
//		$data['checkStatus'] = isset($_GET['s']) ? $_GET['s'] : '';
		$data['pageSize'] = 15;
		$data['pageNum'] = isset($_GET['page'])?$_GET['page']:'1';
		$html = send_post('http://192.168.1.207:8003/manager/company/queryCompanyBasicInfo',$data);
		//print_r($html);exit;
		$count = $html['data']['totalRowNumber'];
		import('ORG.Util.Page');				// 导入分页类
		$pagenum = isset ( $_REQUEST ['page'] ) ? $_REQUEST ['page'] : 1;
		$page_row = 15;//每页显示条数
		//dump($count);
		$p = new Page ($count,$page_row,8);
		$p->isJumpPage = true;
		$p->setConfig('theme','%totalRow% %header% %nowPage%/%totalPage% 页%first% %upPage% %linkPage% %downPage% %end% %jump%');
		$show = $p->show();
		$this->assign('page',$show);
		$this->assign('local',$html['data']['dataList']);
		$this->assign('key',$data['key']);
		$this->assign('url',C('URL'));
		$this->assign('checkName',$_SESSION['ThinkUser']['Username']);
		$this->assign('userId',$_SESSION['ThinkUser']['ID']);
		$this->display();
	}



	/**
	 * 查看入驻信息资料
	 */
	public function checklist(){
		$data['sid']=$_GET['sid'];
		$html = send_post('http://192.168.1.207:8003/manager/company/getCompanyBasicInfo',$data);
		//print_r($html['data']);exit;
		$list=json_decode(json_encode($html['data'],true),true);
		//print_r($list);exit;
		$this->assign('data',$list);
		$this->display();
	}


	/**
	 * 查看审核记录
	 */
	public function checkrecord(){
		$data['companyInfoSid']=$_GET['sid'];
		$html = send_post('http://192.168.1.207:8003/manager/company/getCheckCompanyInfoRecord',$data);
		//print_r($html);exit;
		$this->assign('data',$html['data']);
		$this->display();
	}


	/**
	 * 审核通过
	 */
	public function checkpass(){
		$data['sid']=$_POST['sid'];
		$data['userId']=$_POST['userId'];
		$data['name']=$_POST['name'];
		//$data['details']=$_POST['content'];
		$data['status']=2;
		//print_r($data);exit;
		$status=send_post('http://192.168.1.207:8003/manager/company/checkCompanyBasicInfo',$data);
		if ($status) {
			return $this->ajaxReturn();
		}
	}

	/**
	 * 审核不通过
	 */
	public function checknopass(){
		if ($this->isPost()) {
			$data['sid']=$_POST['sid'];
			$data['userId']=$_POST['userId'];
			$data['name']=$_POST['name'];
			$data['details']=$_POST['content'];
			$data['status']=0;
			$status=send_post('http://192.168.1.207:8003/manager/company/checkCompanyBasicInfo',$data);
			if ($status) {
				//parent::operating(__ACTION__,0,'提交成功');
				$this->success('提交成功','check',2);
			}else {
				//parent::operating(__ACTION__,1,'提交失败');
				$this->error('提交失败');
			}
		}
		$this->assign('sid',$_GET['sid']);
		$this->assign('userId',$_GET['userId']);
		$this->assign('name',$_GET['name']);
		$this->display();
	}

}
?>

